package com.vichat.res.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.vichat.common.cache.RedisHelper;
import com.vichat.common.constant.GlobalConstant;
import com.vichat.common.util.DateUtil;
import com.vichat.common.util.FileTypeUtil;
import com.vichat.common.util.NumberUtils;
import com.vichat.common.util.UserUtil;
import com.vichat.common.vo.PageFilter;
import com.vichat.core.dao.BaseDaoI;
import com.vichat.res.entity.*;
import com.vichat.res.service.IResService;
import com.vichat.res.vo.*;
import com.vichat.user.entity.UserBasic;
import com.vichat.user.entity.UserOrg;
import com.vichat.user.service.IUserService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.*;

@Service("resService")
public class ResServiceImpl implements IResService {
    private static transient final Logger logger = LoggerFactory.getLogger(ResServiceImpl.class);

    @Autowired
    private BaseDaoI<RoomResource> resourceDao;
    @Autowired
    private BaseDaoI<ResFile> resFileDao;
    @Autowired
    private BaseDaoI<ResConfig> configDao;
    @Autowired
    private BaseDaoI<RoomMicRecordDLog> roomMicRecordDLogDao;
    @Autowired
    private BaseDaoI<ResEvent> eventDao;
    @Autowired
    private BaseDaoI<ResRelat> relatDao;
    @Autowired
    private IUserService userService;

    @Override
    public RoomResourceVO saveRoomResource(RoomResourceVO roomResourceVO) {
        RoomResource roomResource = roomResourceVO.getRoomResource();
        ResFile resFile = roomResourceVO.getResFile();
        logger.error("频道资源UID=" + roomResource.getUid());
        if (NumberUtils.isNullOrZero(resFile.getResId())) {
            resFileDao.save(resFile);
        }
        if (NumberUtils.isNullOrZero(roomResource.getRecId())) {
            roomResource.setResId(resFile.getResId());
            resourceDao.save(roomResource);
        }
        return roomResourceVO;
    }

    /**
     * 获取频道资源详情
     *
     * @param uid
     * @param rid
     * @param startTime
     * @param endTime
     * @param address
     * @param page
     * @param rows
     * @return
     * @throws Exception
     */
    @Override
    public List<FileResourceRoomVO> getRoomResourceDetail(long uid, long rid, String startTime, String endTime, String address, int page, int rows) throws Exception {
        List<RoomResource> channelResources = this.getRoomResources(uid, rid, startTime, endTime, address, page, rows);
        List<FileResourceRoomVO> edposResourceChannelVOs = new ArrayList<>();
        for (RoomResource val : channelResources) {
            val.setUserName(RedisHelper.getUname(val.getUid()));
            FileResourceRoomVO edposResourceChannelVO = this.getFileResourceRoomById(val.getRecId());
            edposResourceChannelVOs.add(edposResourceChannelVO);
        }
        return edposResourceChannelVOs;
    }

    /**
     * 根据ID获取频道资源详情
     *
     * @param recId
     * @return
     * @throws Exception
     */
    public FileResourceRoomVO getFileResourceRoomById(long recId) throws Exception {
        FileResourceRoomVO resourceChannelVO = new FileResourceRoomVO();
        List<TerminalFileVO> terminalFileList = new ArrayList<>();
        RoomResource roomResource = resourceDao.get(RoomResource.class, recId);
        ResFile resFile = this.getResFileById(roomResource.getResId());
        if (resFile != null) {
            TerminalFileVO terminalFileVO = new TerminalFileVO();
            terminalFileVO.setResId(roomResource.getResId());
            terminalFileVO.setStoreSrc(resFile.getStoreSrc());
            terminalFileVO.setStoreUrl(resFile.getStoreUrl());
            terminalFileVO.setResType(resFile.getResType());
            terminalFileList.add(terminalFileVO);
        }
        resourceChannelVO.setRoomResource(roomResource);
        resourceChannelVO.setTerminalFileList(terminalFileList);
        return resourceChannelVO;
    }


    /**
     * 获取频道资源详情总数
     *
     * @param uid
     * @param rid
     * @param startTime
     * @param endTime
     * @param address
     * @return
     * @throws Exception
     */
    @Override
    public Long getRoomResourceDetailCount(long uid, long rid, String startTime, String endTime, String address) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        String hql = "select count(*) from resdb.room_resource where state = 1";
        if (NumberUtils.isNotNullOrZero(rid)) {
            hql += " and rid = :rid";
            params.put("rid", rid);
        }
        if (NumberUtils.isNotNullOrZero(uid)) {
            hql += " and uid = :uid";
            params.put("uid", uid);
        }
        if (StringUtils.isNotBlank(startTime)) {
            hql += " and create_time >= :startTime";
            params.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            hql += " and create_time <= :endTime";
            params.put("endTime", endTime);
        }
        return resourceDao.count(hql, params);
    }

    @Override
    public List<RoomResource> getRoomResources(long uid, long rid, String startTime, String endTime, String resType, int page, int rows) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        String sql = "select a.*,c.resource_date from resdb.room_resource a,resdb.res_file c where a.res_id=c.res_id ";
        if (NumberUtils.isNotNullOrZero(rid)) {
            sql += " and a.rid = :rid";
            params.put("rid", rid);
        }
        if (NumberUtils.isNotNullOrZero(uid)) {
            sql += " and a.uid = :uid";
            params.put("uid", uid);
        }
        if (StringUtils.isNotBlank(startTime)) {
            sql += " and resource_date >= :startTime";
            params.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            sql += " and resource_date <= :endTime";
            params.put("endTime", endTime);
        }
        if (StringUtils.isNotBlank(resType)) {
            sql += " and res_type = :resType";
            params.put("resType", resType);
        }
        sql += " order by resource_date desc";
        return resourceDao.findEntityBySql(sql, params, RoomResource.class, page, rows);
    }

    @Override
    public List<RoomResource> getResoucesByType(long partyId, long relatType) {
        return null;
    }

    @Override
    public long getResouceUsageSizeSum(long partyId) {
        return 0;
    }

    public List<VichatRoomRecResVO> getVichatRoomRecResources(long uid, long rid, String orgCode, String startTime, String endTime, String resType, long partyId,int page, int rows) throws Exception {
        Map<String, Object> params = new HashMap<String, Object>();
        String sql = "select a.rid,a.uid,a.user_name as userName,a.org_id as orgId,a.org_code as orgCode,c.res_type as resType,c.store_url as storeUrl,c.resource_date as resourceDate from resdb.room_resource a,resdb.res_file c where a.res_id=c.res_id ";
        if (NumberUtils.isNotNullOrZero(rid)) {
            sql += " and a.rid = :rid";
            params.put("rid", rid);
        }
        if (NumberUtils.isNotNullOrZero(uid)) {
            sql += " and a.uid = :uid";
            params.put("uid", uid);
        }
        if (NumberUtils.isNotNullOrZero(partyId)) {
            sql += " and a.party_id = :partyId";
            params.put("partyId", partyId);
        }
        if (StringUtils.isNotBlank(orgCode)) {
            sql += " and a.org_code like :orgCode";
            params.put("orgCode", orgCode + "%");
        }
        if (StringUtils.isNotBlank(startTime)) {
            sql += " and resource_date >= :startTime";
            params.put("startTime", startTime);
        }
        if (StringUtils.isNotBlank(endTime)) {
            sql += " and resource_date <= :endTime";
            params.put("endTime", endTime);
        }
        if (StringUtils.isNotBlank(resType)) {
            sql += " and c.res_type = :resType";
            params.put("resType", resType);
        }
        sql += " order by resource_date desc";
        return resourceDao.findVoBySql(sql, params, VichatRoomRecResVO.class, page, rows);
    }


    /**
     * 获取资源
     *
     * @param resId
     * @return
     */
    public ResFile getResFileById(long resId) {
        return resFileDao.get(ResFile.class, resId);
    }

    @Override
    public void addRoomResource(RecFileVO recFile) throws Exception {
        RoomResourceVO vo = new RoomResourceVO();

        RoomResource roomResource = new RoomResource();
        roomResource.setRid(recFile.getRid());
        logger.error("文件用户UID=" + recFile.getUid());
        roomResource.setUid(recFile.getUid());

        try {
            long orgId = 0l;
            String orgCode = "";
            long partyId = 0l;
            String userName = "";
            String objstr = RedisHelper.getUser(recFile.getUid());
            UserBasic obj = JSONObject.parseObject(objstr,UserBasic.class);
            if(obj != null){
                orgCode = obj.getOrgCode();
                orgId = obj.getOrgId();
                partyId = obj.getPartyId();
                userName = obj.getUserName();
            }
            roomResource.setPartyId(partyId);
            roomResource.setUserName(userName);
            roomResource.setOrgId(orgId);
            roomResource.setOrgCode(orgCode);
        } catch (Exception e) {
            logger.error("addRoomResource:" + e.getMessage());
        }


        String ext = FileTypeUtil.getExtensionName(recFile.getFileName());
        roomResource.setRemark(recFile.getRecdTime() + "." + ext);
        ResFile resource = newResFile(recFile);


        vo.setRoomResource(roomResource);
        vo.setResFile(resource);
        RoomResourceVO resourceVO = this.saveRoomResource(vo);

//        //视频事件
//        try {
//            if (StringUtils.equals(resource.getResType(), "mp4")) {
//                EdposEvent mp4Event = new EdposEvent();
//                mp4Event.setPartyId(resource.getPartyId());
//                mp4Event.setOrgId(channelResource.getOrgId());
//                mp4Event.setOrgCode(channelResource.getOrgCode());
//                mp4Event.setUid(channelResource.getUid());
//                mp4Event.setUserName(channelResource.getUserName());
//                mp4Event.setEvtTypeId(GlobalConstant.EVENT_6);
//                mp4Event.setMapLongi(resource.getLongitude());
//                mp4Event.setMapLati(resource.getLatitude());
//                mp4Event.setAddress(resource.getAddress());
//                mp4Event.setEventDesc("分享视频");
//                EdposEvent videoEvent = srvEdposEventService.saveEdposEvent(mp4Event);
//                //保存视频资源关系
//            }
//        } catch (Exception e) {
//            logger.error("保存视频事件异常");
//        }
    }

    private ResFile newResFile(RecFileVO recFile) {
        ResFile resource = new ResFile();
        String fileName = recFile.getFileName();
        StringBuilder storeUrl = new StringBuilder();
        if(recFile.getUrl().contains("/file/record/")){
            storeUrl.append(recFile.getUrl());
        }else {
            storeUrl.append(recFile.getUrl()).append("recd/").append(recFile.getDir()).append(fileName);
        }
        resource.setStoreUrl(storeUrl.toString());
        resource.setStoreSrc("RECD");
        resource.setSize(recFile.getFileSize());
        resource.setResName(fileName);

        resource.setResourceDate(DateUtil.getDateByString(recFile.getRecdTime(), "yyyyMMddHHmmss"));
        resource.setResOriginalName(fileName);
        String extName = FileTypeUtil.getExtensionName(fileName);
        resource.setResType(extName);
        resource.setStoreDate(DateUtil.getFormattedDate(new Date(), "yyyyMMdd"));

//        long uid = recFile.getUid();
//        String locationDataJson = RedisHelper.getUserLocationData(uid);
//        logger.info("locationDataJson:" + locationDataJson);
//        if (StringUtils.isNotBlank(locationDataJson)) {
//            JSONObject locationData = JSONObject.parseObject(locationDataJson);
//
//            if (locationData.getDate("gatherTime") != null) {
//                long gatherTime = locationData.getDate("gatherTime").getTime();
//                long currentTime = DateUtil.getCurrTime();
//                logger.info("gatherTime:" + gatherTime);
//                logger.info("currentTime:" + currentTime);
//            }
//
//
//        }

        return resource;
    }

    /**
     * 保存修改资源信息
     *
     * @param file
     * @throws Exception
     */
    public ResFile saveOrUpdResFile(ResFile file) throws Exception {
        resFileDao.save(file);
        return file;
    }

    /**
     * 获取资源配置
     *
     * @param code
     * @return
     * @throws Exception
     */
    public ResConfig getResConfig(String code) throws Exception {
        return configDao.get(ResConfig.class, code);
    }

    public void processRoomMicRecord(RoomMicRecordDLog roomMicRecordDLog) {
        HashMap params = new HashMap();
        StringBuilder sql = new StringBuilder();
        sql.append("insert into ").append(this.roomMicRecordDTablename(roomMicRecordDLog.getEndTime()));
        sql.append(" (uid,rid,party_id,start_time,end_time) values (:uid, :rid, :partyId, :startTime, :endTime)");
        params.put("uid", roomMicRecordDLog.getUid());
        params.put("rid", roomMicRecordDLog.getRid());
        params.put("partyId", roomMicRecordDLog.getPartyId());
        params.put("startTime", roomMicRecordDLog.getStartTime());
        params.put("endTime", roomMicRecordDLog.getEndTime());
        roomMicRecordDLogDao.executeSql(sql.toString(), params);
    }

    private String roomMicRecordDTablename(Date date) {
        String name = DateUtil.getYearMonthDay(date);
        return "resdb.room_mic_record_d_log_" + name;
    }

    public List<RoomMicRecordDLog> queryRoomMicRecord(RoomMicRecordDLog queryBean, PageFilter pageFilter) {
        String sql = "select * from " + roomMicRecordDTablename(queryBean.getQueryDate()) + " where 1=1 ";
        HashMap params = new HashMap();
        return roomMicRecordDLogDao.findEntityBySql(sql + queryRoomMicRecordWhereSql(queryBean, params) + " order by id desc", params, RoomMicRecordDLog.class, pageFilter.getPage(), pageFilter.getRows());
    }

    public BigInteger countRoomMicRecord(RoomMicRecordDLog queryBean) {
        String sql = "select count(*) from " + roomMicRecordDTablename(queryBean.getQueryDate()) + " where 1=1 ";
        HashMap params = new HashMap();
        return roomMicRecordDLogDao.countBySql(sql + queryRoomMicRecordWhereSql(queryBean, params), params);
    }

    private String queryRoomMicRecordWhereSql(RoomMicRecordDLog queryBean, HashMap params) {
        StringBuilder sql = new StringBuilder();
        if (NumberUtils.isNotNullOrZero(queryBean.getPartyId())) {
            sql.append(" and party_id = :partyId");
            params.put("partyId", queryBean.getPartyId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getUid())) {
            sql.append(" and uid = :uid");
            params.put("uid", queryBean.getUid());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getRid())) {
            sql.append(" and rid = :rid");
            params.put("rid", queryBean.getRid());
        }
        if (queryBean.getStartTime() != null) {
            sql.append(" and start_time >= :startTime");
            params.put("startTime", queryBean.getStartTime());
        }
        if (queryBean.getEndTime() != null) {
            sql.append(" and end_time <= :endTime");
            params.put("endTime", queryBean.getEndTime());
        }

        return sql.toString();
    }

    public ResEvent saveResEvent(ResFileRequsetVO paramsVO, List<ResFileVO> responseRes) {
        ResEvent resEvent = new ResEvent();
        resEvent.setUid(paramsVO.getUid());
        resEvent.setEvtInfo(paramsVO.getEvtInfo());
        resEvent.setEvtType(paramsVO.getEvtType());
        resEvent.setRemindUids(paramsVO.getRemindUids());
        resEvent.setRptUid(paramsVO.getRptUid());
        resEvent.setPartyId(paramsVO.getPartyId());
        resEvent.setDistrictId(paramsVO.getDistrictId());
        resEvent.setEvtResult(paramsVO.getEvtResult());
        resEvent.setRecordType(paramsVO.getRecordType());
        resEvent.setUname(RedisHelper.getUname(paramsVO.getUid()));
        if (NumberUtils.isNotNullOrZero(paramsVO.getRptOrgId())) {
            UserOrg org = userService.queryUserOrgByOrgId(paramsVO.getRptOrgId());
            if (org != null) {
                resEvent.setRptOrgId(org.getOrgId());
                resEvent.setRptOrgCode(org.getOrgCode());
            }
        }
        if (NumberUtils.isNotNullOrZero(paramsVO.getcUid())) {
            resEvent.setCUid(paramsVO.getcUid());
            resEvent.setCUname(RedisHelper.getUname(paramsVO.getcUid()));
        }
        resEvent.setLongitude(paramsVO.getLongitude());
        resEvent.setLatitude(paramsVO.getLatitude());
        resEvent.setAddress(paramsVO.getAddress());
        eventDao.save(resEvent);

        if (responseRes != null && !responseRes.isEmpty()) {
            for (ResFileVO fileVO : responseRes) {
                ResRelat resRelat = new ResRelat();
                resRelat.setObjId(resEvent.getEvtId());
                resRelat.setResId(fileVO.getResId());
                resRelat.setObjType(GlobalConstant.RES_FILE_OBJ_TYPE_1);
                relatDao.save(resRelat);
            }
        }

        if (StringUtils.isNotBlank(paramsVO.getRemindUids())) {
            String uidJson = UserUtil.getUidsToJson(paramsVO.getRemindUids());
            resEvent.setRemindUids(uidJson);
            eventDao.update(resEvent);
        }

        return resEvent;
    }
    public void saveResByType(ResFileRequsetVO paramsVO,List<ResFileVO> responseRes){
        List<ResFile> oldFileList = getFileListByType(paramsVO.getObjId(),paramsVO.getObjType());
        if(oldFileList != null && !oldFileList.isEmpty()){
            String sql = "delete from taskdb.res_relat where obj_id=" + paramsVO.getObjId() + " and obj_type=" + paramsVO.getObjType();
            relatDao.executeSql(sql);
        }

        if (responseRes != null && !responseRes.isEmpty()) {
            for (ResFileVO fileVO : responseRes) {
                ResRelat resRelat = new ResRelat();
                resRelat.setObjId(paramsVO.getObjId());
                resRelat.setResId(fileVO.getResId());
                resRelat.setObjType(paramsVO.getObjType());
                relatDao.save(resRelat);
            }
        }
    }

    public ResEvent getResEventByEvtId(long evtId) {
        return eventDao.get(ResEvent.class, evtId);
    }

    public void delResEvent(long eventId) {
        ResEvent event = eventDao.get(ResEvent.class, eventId);
        if (event != null) {
            eventDao.delete(event);

            List<ResFile> resFileList = this.getFileListByEventId(eventId);
            if (resFileList != null) {
                for (ResFile resFile : resFileList) {
                    resFileDao.delete(resFile);
                }
            }
        }
    }

    public List<ResEvent> queryResEvent(ResEvent queryBean, PageFilter pageFilter) {
        String hql = "from ResEvent where state = 1 ";
        HashMap params = new HashMap();
        List<ResEvent> eventList = eventDao.find(hql + queryResEventWhereHql(queryBean, params) + " order by evtId desc ", params, pageFilter.getPage(), pageFilter.getRows());
        if (eventList != null) {
            for (ResEvent resEvent : eventList) {
                //文件
                List<ResFile> files = this.getFileListByEventId(resEvent.getEvtId());
                if (files != null) {
                    List<ResFileVO> resFileVOS = new ArrayList<>();
                    for (ResFile file : files) {
                        ResFileVO fileVO = new ResFileVO();
                        fileVO.setResId(file.getResId());
                        fileVO.setStoreUrl(file.getStoreUrl());
                        fileVO.setResType(file.getResType());
                        fileVO.setL(file.getL());
                        resFileVOS.add(fileVO);
                    }
                    resEvent.setFileVOList(resFileVOS);
                }
                resEvent.setUserName(RedisHelper.getUname(resEvent.getUid()));
            }
        }

        return eventList;
    }

    public long countResEvent(ResEvent queryBean) {
        String hql = "select count(*) from ResEvent where state = 1 ";
        HashMap params = new HashMap();
        return eventDao.count(hql + queryResEventWhereHql(queryBean, params), params);
    }

    private String queryResEventWhereHql(ResEvent queryBean, HashMap params) {
        String hql = "";
        if (NumberUtils.isNotNullOrZero(queryBean.getPartyId())) {
            hql += " and partyId = :partyId";
            params.put("partyId", queryBean.getPartyId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getEvtType())) {
            hql += " and evtType = :evtType";
            params.put("evtType", queryBean.getEvtType());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getCUid())) {
            hql += " and cUid = :cUid";
            params.put("cUid", queryBean.getCUid());
        }
        if (StringUtils.isNotBlank(queryBean.getAddress())) {
            hql += " and address like :address";
            params.put("address", "%" + queryBean.getAddress() + "%");
        }
        if (StringUtils.isNotBlank(queryBean.getEvtInfo())) {
            hql += " and evtInfo like :evtInfo";
            params.put("evtInfo", "%" + queryBean.getEvtInfo() + "%");
        }
        if (queryBean.getStartDate() != null) {
            hql += " and createTime >=  :startDate";
            params.put("startDate", queryBean.getStartDate());
        }
        if (queryBean.getEndDate() != null) {
            hql += " and createTime <=  :endDate";
            params.put("endDate", queryBean.getEndDate());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getRptUid()) && StringUtils.isNotBlank(queryBean.getRptOrgCode())) {
            hql += " and ((rptUid = :rptUid or rptOrgCode like :rptOrgCode or remindUids like :remindUids)";
            params.put("rptUid", queryBean.getRptUid());
            params.put("rptOrgCode", queryBean.getRptOrgCode() + "%");
            params.put("remindUids", "%" + queryBean.getRemindUids() + "%");

            if (NumberUtils.isNotNullOrZero(queryBean.getQueryUid())) {
                hql += ")";
                hql += " and uid = :uid";
                params.put("uid", queryBean.getQueryUid());
            } else {
                hql += " or uid = :uid";
                params.put("uid", queryBean.getUid());
                hql += ")";
            }
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getQueryUid())) {
            hql += " and uid = :uid";
            params.put("uid", queryBean.getUid());
        }
        return hql;
    }

    public List<ResEvent> queryResEventRecording(ResEvent queryBean, PageFilter pageFilter) {
        String hql = "select a.* from taskdb.res_event a,userdb.user_basic b where a.state = 1 and a.uid = b.uid and a.evt_type = 2 ";
        HashMap params = new HashMap();
        List<ResEvent> eventList = eventDao.findEntityBySql(hql + queryResEventRecordingWhereHql(queryBean, params) + " order by a.evt_id desc ", params, ResEvent.class, pageFilter.getPage(), pageFilter.getRows());
        if (eventList != null) {
            for (ResEvent resEvent : eventList) {
                //文件
                List<ResFile> files = this.getFileListByEventId(resEvent.getEvtId());
                if (files != null) {
                    List<ResFileVO> resFileVOS = new ArrayList<>();
                    for (ResFile file : files) {
                        ResFileVO fileVO = new ResFileVO();
                        fileVO.setResId(file.getResId());
                        fileVO.setStoreUrl(file.getStoreUrl());
                        fileVO.setResType(file.getResType());
                        fileVO.setL(file.getL());
                        resFileVOS.add(fileVO);
                    }
                    resEvent.setFileVOList(resFileVOS);
                }
                //用户信息
                resEvent.setUserName(RedisHelper.getUname(resEvent.getUid()));
            }
        }

        return eventList;
    }

    public BigInteger countResEventRecording(ResEvent queryBean) {
        String hql = "select count(a.evt_id) from taskdb.res_event a,userdb.user_basic b where a.state = 1 and a.uid = b.uid and a.evt_type = 2 ";
        HashMap params = new HashMap();
        return eventDao.countBySql(hql + queryResEventRecordingWhereHql(queryBean, params), params);
    }

    private String queryResEventRecordingWhereHql(ResEvent queryBean, HashMap params) {
        String hql = "";
        if (NumberUtils.isNotNullOrZero(queryBean.getPartyId())) {
            hql += " and a.party_id = :partyId";
            params.put("partyId", queryBean.getPartyId());
        }
        if (NumberUtils.isNotNullOrZero(queryBean.getOrgId())) {
            UserOrg orgFDB = userService.queryUserOrgByOrgId(queryBean.getOrgId());
            if (orgFDB.getParentId() != 0) {
                hql += " and b.org_code like :orgCode";
                params.put("orgCode", orgFDB.getOrgCode() + "%");
            }
        }
        if (StringUtils.isNotBlank(queryBean.getCUname())) {
            hql += " and a.c_uname like :cUname";
            params.put("cUname", "%" + queryBean.getCUname() + "%");
        }
        if (StringUtils.isNotBlank(queryBean.getUname())) {
            hql += " and a.uname like :uname";
            params.put("uname", "%" + queryBean.getUname() + "%");
        }
        if (queryBean.getStartDate() != null) {
            hql += " and a.create_time >=  :startDate";
            params.put("startDate", queryBean.getStartDate());
        }
        if (queryBean.getEndDate() != null) {
            hql += " and a.create_time <=  :endDate";
            params.put("endDate", queryBean.getEndDate());
        }
        if (queryBean.getRecordType() != null) {
            hql += " and a.record_type =  :recordType";
            params.put("recordType", queryBean.getRecordType());
        }
        return hql;
    }

    public List<ResFile> getFileListByEventId(long evtId) {
        String hql = "select b from ResEvent a,ResFile b,ResRelat c where c.objType = 1 and a.state = 1 and b.state = 1 and a.evtId = c.objId and b.resId = c.resId and a.evtId = :evtId";
        HashMap params = new HashMap();
        params.put("evtId", evtId);
        return resFileDao.find(hql, params);
    }
    public List<ResFile> getFileListByType(long objId,long objType) {
        String hql = "select b from ResFile b,ResRelat c where c.objType = :objType and b.state = 1 and b.resId = c.resId and c.objId = :objId";
        HashMap params = new HashMap();
        params.put("objId", objId);
        params.put("objType", objType);
        return resFileDao.find(hql, params);
    }

}
